﻿using PickingGuide.Business;
using PickingGuide.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;

namespace PickingGuideForm
{
    public partial class FrmManualSetup : Form
    {
        public Action RefreshUiEvent;
        PickingSetupBLL pickingSetupBLL = new PickingSetupBLL();
        ManualSetupBLL manualSetupBLL = new ManualSetupBLL();
        ManualSetupModel _manualSetupModel;

        public FrmManualSetup()
        {
            InitializeComponent();
            var list1 = pickingSetupBLL.GetList(1);
            var list2 = pickingSetupBLL.GetList(2);

            cbPicking1.DataSource = list1;
            cbPicking1.DisplayMember = "Name";
            cbPicking1.ValueMember = "ID";
            cbPicking1.SelectedIndex = 0;

            if(list2 != null && list2.Count>0)
            {
                cbPicking2.DataSource = list2;
                cbPicking2.DisplayMember = "Name";
                cbPicking2.ValueMember = "ID";
                cbPicking2.SelectedIndex = 0;
            }else
            {
                cbPicking2.Enabled = false;
            }
        }

        public FrmManualSetup(ManualSetupModel model)
        {
            InitializeComponent();
            _manualSetupModel = model;

            var list1 = pickingSetupBLL.GetList(1);
            var list2 = pickingSetupBLL.GetList(2);
            //var picking1 = pickingSetupBLL.GetModel(_manualSetupModel.Picking1ID);
            //var picking2 = pickingSetupBLL.GetModel(_manualSetupModel.Picking2ID);

            cbPicking1.DataSource = list1;
            cbPicking1.DisplayMember = "Name";
            cbPicking1.ValueMember = "ID";
            cbPicking1.SelectedValue = _manualSetupModel.Picking1ID;
            cbPicking1.Refresh();

            if (list2 != null && list2.Count > 0)
            {
                cbPicking2.DataSource = list2;
                cbPicking2.DisplayMember = "Name";
                cbPicking2.ValueMember = "ID";
                cbPicking2.SelectedValue = _manualSetupModel.Picking2ID;
                cbPicking2.Refresh();
            }
            else
            {
                cbPicking2.Enabled = false;
            }

            txtName.Text = _manualSetupModel.Name;  

            btnSave.Text = "修改";
        }

        private void FrmManualSetup_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("拣货名称不能为空");
                return;
            }
            if (_manualSetupModel == null)
            {
                //var model = rackSetupBLL.GetModel(txtRackName.Text);
                //if (model != null)
                //{
                //    MessageBox.Show("已存在相同的货架配置，不能重复！");
                //    return;
                //}
                _manualSetupModel = new ManualSetupModel();

                var picking1 = cbPicking1.SelectedItem as PickingSetupModel;
                if (cbPicking2.Enabled == true)
                {
                    var picking2 = cbPicking2.SelectedItem as PickingSetupModel;
                    _manualSetupModel.Picking2ID = picking2.ID;
                }

                _manualSetupModel.Picking1ID = picking1.ID;        
                _manualSetupModel.Name = txtName.Text.Trim();
                var result = manualSetupBLL.Add(_manualSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加手动拣货配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加手动拣货配置失败：" + result.Dsca);
            }
            else
            {
                var picking1 = cbPicking1.SelectedItem as PickingSetupModel;
                _manualSetupModel.Picking1ID = picking1.ID;
                if (cbPicking2.Enabled == true)
                {
                    var picking2 = cbPicking2.SelectedItem as PickingSetupModel;
                    _manualSetupModel.Picking2ID = picking2.ID;
                }

                _manualSetupModel.Name = txtName.Text.Trim();

                var result = manualSetupBLL.Update(_manualSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改手动拣货配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改手动拣货配置失败：" + result.Dsca);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
